package com.healthcarex.hip.saas.common.util;

import java.lang.reflect.Field;

/**
 * *******description*******
 * <p>
 * *************************
 *
 * @author ChenPan
 * @date 2018/8/27 09:46
 */
public class BeanUtils {

    /**
     * 将空字符串属性的字段置为null
     *
     * @param source
     * @param <T>
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static <T> T setNullValue(T source) throws IllegalArgumentException, IllegalAccessException {
        Field[] fields = source.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.getGenericType().toString().equals(
                    "class java.lang.String")) {
                field.setAccessible(true);
                Object obj = field.get(source);
                if (obj != null && obj.equals("")) {
                    field.set(source, null);
                }
            }
        }
        return source;
    }
}
